<template>
  <!-- 熔炼日记录表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">日期</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.smeltDate }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">计划炉数</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.planStoveCount }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">实际炉数</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.actualStoveCount }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">熔炼待铁水时间（分钟）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.smeltStayWater }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">铁水吨电耗</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.waterInitialCurrentDrain }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">除渣剂</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialDregsRemover }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">生铁</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialPigIronQuantity }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">高锰废钢</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialHighSteel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">中锰废钢</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialMediumSteel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">低锰废钢</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialLowSteel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">铜</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialMetalCopper }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">锡</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialMetalTin }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">锑</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialMetalStibium }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">钼</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialMetalMolybdenum }}
        </div>
      </el-descriptions-item>
      <!--<el-descriptions-item>
        <template #label>
          <div class="cell-item">期初原材料</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialMaterial }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">期初稀有金属使用量</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.initialMetalUsageAmount }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">铁水吨期末电耗</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.waterFinalCurrentDrain }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">期末除渣剂</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.finalDregsRemover }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">期末原材料</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.finalMaterial }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">期末稀有金属使用量</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.finalMetalUsageAmount }}
        </div>
      </el-descriptions-item>-->
    </el-descriptions>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="260"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="日期" prop="smeltDate" label-width="140px">
            <!-- <el-input
              v-model="formData.smeltDate"
              placeholder="请输入日期"
              clearable
            /> -->
            <el-date-picker
              v-model="formData.smeltDate"
              type="date"
              placeholder="请输入日期"
              style="width: 100%"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
              :disabled="props.isReadonly"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="计划炉数"
            prop="planStoveCount"
            label-width="140px"
          >
            <el-input
              v-model="formData.planStoveCount"
              placeholder="请输入计划炉数"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="实际炉数"
            prop="actualStoveCount"
            label-width="140px"
          >
            <el-input
              v-model="formData.actualStoveCount"
              placeholder="请输入实际炉数"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="熔炼待铁水时间"
            prop="smeltStayWater"
            label-width="140px"
          >
            <el-input
              v-model="formData.smeltStayWater"
              placeholder="请输入熔炼待铁水时间（分钟）"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="铁水吨电耗"
            prop="waterInitialCurrentDrain"
            label-width="140px"
          >
            <el-input
              v-model="formData.waterInitialCurrentDrain"
              placeholder="请输入铁水吨电耗"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="除渣剂"
            prop="initialDregsRemover"
            label-width="140px"
          >
            <el-input
              v-model="formData.initialDregsRemover"
              placeholder="请输入除渣剂"
              clearable
            />
          </el-form-item>
        </el-col>
        <!--<el-col :span="12">
          <el-form-item
            label="期初原材料"
            prop="initialMaterial"
            label-width="140px"
          >
            <el-input
              v-model="formData.initialMaterial"
              placeholder="请输入期初原材料：生铁、高锰废钢、低锰废钢、中锰废钢"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="期初稀有金属使用量"
            prop="initialMetalUsageAmount"
            label-width="140px"
          >
            <el-input
              v-model="formData.initialMetalUsageAmount"
              placeholder="请输入期初稀有金属使用量：铜、锡、锑、钼"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="铁水吨期末电耗"
            prop="waterFinalCurrentDrain"
            label-width="140px"
          >
            <el-input
              v-model="formData.waterFinalCurrentDrain"
              placeholder="请输入铁水吨期末电耗"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="期末除渣剂"
            prop="finalDregsRemover"
            label-width="140px"
          >
            <el-input
              v-model="formData.finalDregsRemover"
              placeholder="请输入期末除渣剂"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="期末原材料"
            prop="finalMaterial"
            label-width="140px"
          >
            <el-input
              v-model="formData.finalMaterial"
              placeholder="请输入期末原材料：生铁、高锰废钢、低锰废钢、中锰废钢"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="期末稀有金属使用量"
            prop="finalMetalUsageAmount"
            label-width="140px"
          >
            <el-input
              v-model="formData.finalMetalUsageAmount"
              placeholder="请输入期末稀有金属使用量：铜、锡、锑、钼"
              clearable
            />
          </el-form-item>
        </el-col>-->
      </el-row>
      <div class="table-header">
        <h3>原材料</h3>
      </div>
      <el-row>
        <el-col :span="6">
          <el-form-item
            label="生铁"
            prop="initialPigIronQuantity"
            label-width="100px"
          >
            <el-input
              v-model="formData.initialPigIronQuantity"
              placeholder="请输入生铁数量"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            label="高锰废钢"
            prop="initialHighSteel"
            label-width="100px"
          >
            <el-input
              v-model="formData.initialHighSteel"
              placeholder="请输入高锰废钢数量"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            label="中锰废钢"
            prop="initialMediumSteel"
            label-width="100px"
          >
            <el-input
              v-model="formData.initialMediumSteel"
              placeholder="请输入中锰废钢数量"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            label="低锰废钢"
            prop="initialLowSteel"
            label-width="100px"
          >
            <el-input
              v-model="formData.initialLowSteel"
              placeholder="请输入低锰废钢数量"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="table-header">
        <h3>稀有金属</h3>
      </div>
      <el-row>
        <el-col :span="6">
          <el-form-item
            label="铜"
            prop="initialMetalCopper"
            label-width="100px"
          >
            <el-input
              v-model="formData.initialMetalCopper"
              placeholder="请输入铜数量"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            label="锡"
            prop="initialMetalTin"
            label-width="100px"
          >
            <el-input
              v-model="formData.initialMetalTin"
              placeholder="请输入锡数量"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            label="锑"
            prop="initialMetalStibium"
            label-width="100px"
          >
            <el-input
              v-model="formData.initialMetalStibium"
              placeholder="请输入锑数量"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            label="钼"
            prop="initialMetalMolybdenum"
            label-width="100px"
          >
            <el-input
              v-model="formData.initialMetalMolybdenum"
              placeholder="请输入钼数量"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESSMELTDAILYRECORDADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  smeltDate: [{ required: true, message: "请选择日期", trigger: "change" }],
  smeltStayWater: [{ pattern: /^\d{1,18}$/, message: "请输入整数" }],
  waterInitialCurrentDrain: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  waterFinalCurrentDrain: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  initialDregsRemover: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  finalDregsRemover: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialMaterial: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  finalMaterial: [{ pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" }],
  initialMetalUsageAmount: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  finalMetalUsageAmount: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialPigIronQuantity: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialHighSteel: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialMediumSteel: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialLowSteel: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialMetalCopper: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialMetalTin: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialMetalStibium: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  initialMetalMolybdenum: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  smeltDate: "",
  planStoveCount: "",
  actualStoveCount: "",
  smeltStayWater: "",
  waterInitialCurrentDrain: "",
  waterFinalCurrentDrain: "",
  initialDregsRemover: "",
  finalDregsRemover: "",
  initialMaterial: "",
  finalMaterial: "",
  initialMetalUsageAmount: "",
  finalMetalUsageAmount: "",
  initialPigIronQuantity: "",
  initialHighSteel: "",
  initialMediumSteel: "",
  initialLowSteel: "",
  initialMetalCopper: "",
  initialMetalTin: "",
  initialMetalStibium: "",
  initialMetalMolybdenum: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        smeltDate,
        planStoveCount,
        actualStoveCount,
        smeltStayWater,
        waterInitialCurrentDrain,
        waterFinalCurrentDrain,
        initialDregsRemover,
        finalDregsRemover,
        initialMaterial,
        finalMaterial,
        initialMetalUsageAmount,
        finalMetalUsageAmount,
        initialPigIronQuantity,
        initialHighSteel,
        initialMediumSteel,
        initialLowSteel,
        initialMetalCopper,
        initialMetalTin,
        initialMetalStibium,
        initialMetalMolybdenum,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        smeltDate,
        planStoveCount,
        actualStoveCount,
        smeltStayWater,
        waterInitialCurrentDrain,
        waterFinalCurrentDrain,
        initialDregsRemover,
        finalDregsRemover,
        initialMaterial,
        finalMaterial,
        initialMetalUsageAmount,
        finalMetalUsageAmount,
        initialPigIronQuantity,
        initialHighSteel,
        initialMediumSteel,
        initialLowSteel,
        initialMetalCopper,
        initialMetalTin,
        initialMetalStibium,
        initialMetalMolybdenum,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.smeltDate = "";
  formData.planStoveCount = "";
  formData.actualStoveCount = "";
  formData.smeltStayWater = "";
  formData.waterInitialCurrentDrain = "";
  formData.waterFinalCurrentDrain = "";
  formData.initialDregsRemover = "";
  formData.finalDregsRemover = "";
  formData.initialMaterial = "";
  formData.finalMaterial = "";
  formData.initialMetalUsageAmount = "";
  formData.finalMetalUsageAmount = "";
  formData.initialPigIronQuantity = "";
  formData.initialHighSteel = "";
  formData.initialMediumSteel = "";
  formData.initialLowSteel = "";
  formData.initialMetalCopper = "";
  formData.initialMetalTin = "";
  formData.initialMetalStibium = "";
  formData.initialMetalMolybdenum = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 熔炼日记录保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/messmeltdailyrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
<style scoped lang="scss">
:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  width: 140px;
}
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 180px;
}
</style>
